package com.lais.record.common.utils;

import com.lais.record.common.auth.UserInfo;
import com.lais.record.common.constant.RecordEnum;
import com.lais.record.common.exception.RecordException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;


/**
 * @Author: luowen
 * @Description: 用户工具类
 * @Date: Create in 22:36 2019/11/28
 */
@Slf4j
public class UserInfoUtil {

    /**
     * 获取用户id
     * @return
     */
    public static String getUserId(){
        UserInfo userInfo = getUserInfo();
        if(StringUtils.isBlank(userInfo.getId())){
            log.error("获取登录用户id--用户没登录或登录过期");
            throw new RecordException(RecordEnum.USER_NO_LOGIN.getCode(),RecordEnum.USER_NO_LOGIN.getMessage());
        }
        return userInfo.getId();
    }

    /**
     * 获取登录用户
     * @return
     */
    public static UserInfo getUserInfo(){
        UserInfo userInfo = UserInfoThreadLocal.get();
        if(userInfo == null){
            log.error("获取登录用户--用户没登录或登录过期");
            throw new RecordException(RecordEnum.USER_NO_LOGIN.getCode(),RecordEnum.USER_NO_LOGIN.getMessage());
        }
        return userInfo;
    }
}
